<?php
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Db;

class Common extends Controller
{
	public $config;
	public $nav;
	function __construct(){
		parent::__construct();
		header("Content-type:text/html; charset=utf-8");
		/*$nav = $this->nav = db('navigation')->where('is_show = 1 and parent_id=0')->order('sort,id desc')->select();
		foreach($nav as $key=>$val){
			if($val['type'] == 1){
                $nav[$key]['link_url'] = url('index/category',array('kid'=>$val['cate_id']));
			}elseif($val['type'] == 2){
                $nav[$key]['link_url'] = url('product/category',array('kid'=>$val['category_id']));
            }else{
                $nav[$key]['link_url'] = $val['url'];
            }
            $data = db('navigation')->where("is_show = 1 AND parent_id = ".$val['id'])->order('sort')->select();
            foreach($data as $k=>$v){
                if($v['type']==1){
                    $data[$k]['link_url'] = url('index/category',array('kid'=>$v['cate_id']));
                }elseif($v['type']==2){
                    $data[$k]['link_url'] = url('product/category',array('kid'=>$v['category_id']));
                }else{
                    $data[$k]['link_url'] = $v['url'];
                }
            }
            $nav[$key]['data'] = $data;
		}*/
        // dump($nav);exit;
		/*$this->config = tpCache('shop_info');
		$this->assign('config',$this->config);
        $this->assign('title',$this->config['site_name']);
        $this->assign('keywords',$this->config['keyword']);
        $this->assign('description',$this->config['description']);

        $route_arr = config('route_arr');
        $this->assign('luyou', $route_arr);*/
        // $article_kid = config('article_kid');
        // $this->assign('article_kid',$article_kid);
        //友情链接
        // $link = db('link')->where("is_hide=0 and kid=1")->order("sort_order")->select();
        // $this->assign("link",$link);
        //顶级产品分类
        // $product_kind_list = db("product_kind")->where("is_hide=0 and parent_id=1")->order("sort_order")->select();
        // $this->assign("product_kind_list",$product_kind_list);

        /*$hotword = explode('|',$this->config['site_hotword']);
        $this->assign('hotword',$hotword);


        $this->clientlog();*/
	}

    /* 访问记录 */
    function clientlog() {
        $not_record_ip = config('not_record_ip');
        $clientip = getIP();
        if(!in_array($clientip, $not_record_ip)){
            $useragent = $_SERVER['HTTP_USER_AGENT'];
        
            $client_info = get_os($useragent)."---".get_broswer($useragent);
            $rawdata_position = get_address_by_ip($clientip);
            $rawdata_position = json_decode($rawdata_position, true);
            $country = $rawdata_position['data']['country'];
            $province = $rawdata_position['data']['region'];
            $city = $rawdata_position['data']['city'];
            $nettype = $rawdata_position['data']['isp'];
            $time = date('y-m-d h:m:s');
            // $data = "来自{$country} {$province} {$city }{$nettype} 的客户端: {$client_info},IP为:{$clientip},在{$time}时刻访问了{$_SERVER['PHP_SELF']}文件，访问路径 {$_SERVER['REQUEST_URI']}\r\n";
            // $filename = $_SERVER['DOCUMENT_ROOT']."/log.log";
            // if (!file_exists($filename)) {
            //     fopen($filename, "w+");
            // }
            // file_put_contents($_SERVER['DOCUMENT_ROOT'].'/log.txt', json_encode($tempLog).PHP_EOL, FILE_APPEND );
            // file_put_contents($filename, $data, FILE_APPEND);
            $log['ip'] = $clientip;
            $log['os'] = get_os($useragent);
            $log['broswer'] = get_broswer($useragent);
            $log['area'] = "{$country} {$province} {$city }{$nettype}";
            $log['visit_url'] = $_SERVER['REQUEST_URI'];
            $log['addtime'] = date('Y-m-d H:i:s', time());

            Db::name('client_log')->insert($log);
        }
        
    }

    
}